package cn.tang.crm.servlet;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import cn.tang.crm.service.ActionServiceManager;
import cn.tang.crm.service.action.AdminActionService;
import cn.tang.crm.service.action.UserActionService;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class BaseServlet extends HttpServlet {
	private static final Logger logger = Logger.getLogger(BaseServlet.class);
	private static final long serialVersionUID = -1665964049557317300L;
	protected AdminActionService adminService = ActionServiceManager
			.get(AdminActionService.class);
	protected UserActionService userService = ActionServiceManager
			.get(UserActionService.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String method = req.getParameter("method");
		if (method == null || "".equals(method))
			method = "home";
		try {
			logger.info("-- invoke method:"+method);
			Method mt = this.getClass().getMethod(method,
					HttpServletRequest.class, HttpServletResponse.class);
			mt.invoke(this, req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

	public void home(HttpServletRequest req, HttpServletResponse resp) {
		forward(req, resp, "/index.jsp");
	}
	
	public void redirect(HttpServletResponse resp,
			String path){
		try {
			resp.sendRedirect(path);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void forward(HttpServletRequest req, HttpServletResponse resp,
			String path) {
		try {
			logger.info(path);
			req.getRequestDispatcher(path).forward(req, resp);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}

	public void responseJson(HttpServletResponse resp, Object data) {
		String tmp = json(data);
		response(resp, tmp);
	}

	public void responseJS(HttpServletResponse resp, Object data) {
		String tmp = "<script language=\"javascript\">" + data.toString()
				+ ";</script>";
		response(resp, tmp);
	}

	public void responseJsAlert(HttpServletResponse resp, Object message) {
		String tmp = "alert(\"" + message + "\")";
		responseJS(resp, tmp);
	}

	public void response(HttpServletResponse resp, Object data) {
		try {
			resp.getWriter().println(data);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String json(Object javaObj) {
		String ret = "{}";
		ObjectMapper mapper = new ObjectMapper();
		try {
			ret = mapper.writeValueAsString(javaObj);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return ret;
	}

}
